do "E:\yungyu\lottery_baby\do\set_environment.do"

clear
set more off

global control1 "i.pers_gender i.houhld_resid_cd pers_income_pre1 pers_asset_pre1 pers_earnings_pre1 pers_labor_pre1 married_pre1"
global control2 "totbaby_pre1"
global control3 "pre*redeem*"

use "$wdata\project_lottery_fertility_b3a6_first_5k.dta", clear

gen anybaby_pre1 = totbaby_pre1 > 0
		
gen prizegrp = .
replace prizegrp = 0 if prize10M < 0.001 // < 10k
replace prizegrp = 1 if inrange(prize10M,0.001,0.005) // 10-50k
replace prizegrp = 2 if inrange(prize10M,0.005,0.01) // 50k-100k
replace prizegrp = 3 if inrange(prize10M,0.01,0.025) // 100k-250k
replace prizegrp = 4 if inrange(prize10M,0.025,0.05) // 250k-500k
replace prizegrp = 5 if inrange(prize10M,0.05,0.1) // 500k-1M
replace prizegrp = 6 if inrange(prize10M,0.1,0.25) // 1-2.5M
replace prizegrp = 7 if inrange(prize10M,0.25,0.5) // 2.5-5M
replace prizegrp = 8 if inrange(prize10M,0.5,1) // 5-10M
replace prizegrp = 9 if inrange(prize10M,1,5) // 10-50M
replace prizegrp = 10 if prize10M >= 5 // >= 50M

forv k = 1(1)10{
	cap gen prizegrp`k' = 0
	replace prizegrp`k' = 1 if prizegrp == `k'
	
	forv j = 3(-1)2{
		cap gen currentxprizegrp`k'xpre`j' = .
		replace currentxprizegrp`k'xpre`j' = .
		replace currentxprizegrp`k'xpre`j' = 0 if prizegrp`k'==0
		replace currentxprizegrp`k'xpre`j' = 0 if prizegrp`k'==1
		replace currentxprizegrp`k'xpre`j' = 1 if prizegrp`k'==1 & current == 1 & norm == -`j'
	}
	forv j = 0(1)6{
		cap gen currentxprizegrp`k'xpost`j' = .
		replace currentxprizegrp`k'xpost`j' = .
		replace currentxprizegrp`k'xpost`j' = 0 if prizegrp`k'==0
		replace currentxprizegrp`k'xpost`j' = 0 if prizegrp`k'==1
		replace currentxprizegrp`k'xpost`j' = 1 if prizegrp`k'==1 & current == 1 & norm == `j'
	}
}	

replace norm_year = norm_year + 3
gen tot_asset = pers_asset + spouse_asset

cap rm "$table/temp/FigC2.xls"
cap rm "$table/temp/FigC2.txt"

reghdfe totbaby currentxprizegrp*xp* $control1 $control2 $control3, a(year age norm#current prizegrp#norm prizegrp#current) cl(houseid)
outreg2 using "$table/temp/FigC2.xls" , append bd(3) sd(3) nocon keep(currentxprizegrp*xp*)

replace norm_year = norm_year - 3

gen n = 1
collapse (count)n (mean)totbaby tot_asset prize10M, by(prizegrp current norm)

keep if norm == -1 | norm == 6

gen post = norm > 0
drop norm

reshape wide totbaby tot_asset, i(current prizegrp prize10M n) j(post)

gen bt = totbaby1 - totbaby0
gen bw = tot_asset0
gen btw = tot_asset1 - tot_asset0

keep current prizegrp prize10M bt bw btw n
reshape wide prize10M bt bw btw n, i(prizegrp) j(current)

gen n = n0+n1
keep prizegrp bt0 bw0 btw0 prize10M1 n
order prizegrp bt0 bw0 btw0 prize10M1 n

drop if prizegrp == 0

save "$table/temp/FigC2_Baseline.dta", replace

import delimited "$table/temp/FigC2.txt", clear

gen grp = substr(v1,strpos(v1,"grp")+3,1) if strpos(v1,"grp") != 0
replace v1 = substr(v1,1,strpos(v1,"grp")+2) + substr(v1,strpos(v1,"grp")+4,.)

gen type = mod(_n,2)
replace grp = grp[_n-1] if grp == ""
replace v1 = v1[_n-1] if v1 == ""

drop in 184
keep in 4/184
replace grp = "1" in 91/92
replace type = 2 in 91/92

gen row = substr(v1,-1,1)
destring row, replace force
replace row = -row if strpos(v1,"pre") != 0
replace row = _n if row == .

keep if strpos(v1,"post6") != 0

replace row = _n
loc N = _N + 2
set obs `N'
replace row = 0 if row == .
sort row
drop row

replace v1 = "" if type == 1
drop type

replace grp = "10" in 21/22

gen prizegrp = grp if v1 != ""
destring prizegrp, replace

gen row = _n
merge m:1 prizegrp using "$table/temp/FigC2_Baseline.dta"
sort row
drop row

gen b = subinstr(v2,"*","",.)
destring b, replace force
gen se = subinstr(v2[_n+1],"(","",.) if b != .
replace se = subinstr(se,")","",.)
destring se, replace force
gen lnprize10M1 = ln(prize10M1)

gen prize = prize10M1/1000

twoway 	(sc b prize, ms(O) color(black)) ///
		(rcap upper lower prize, color(black)), ///
		legend(order(1 "Point Estimate" 2 "95% CI")) ///
		xtitle(Mean Prize Amount in Bin (Million NTD)) ///
		ytitle(Effect of Winning Lottery Prize on Fertility) ///
		ylabel(-0.1(0.1)0.5, angle(0) format(%4.1f)) ///
		xlabel(0(50)300,format(%9.0fc)) ///
		scheme(s1color) yline(0, lc(black)) 
graph export "$folder/figure/FigC2a.eps", as(eps) replace
graph export "$folder/figure/FigC2a.eps", as(eps) replace		
		

twoway 	(sc b lnprize, ms(O) color(black)) ///
		(rcap upper lower lnprize, color(black)), ///
		legend(order(1 "Point Estimate" 2 "95% CI")) ///
		xtitle(Mean Prize Amount in Bin (Log Scale)) ///
		xlabel(3.912023 "50K" 6.2146081 "500K" 8.5171932 "5M" 10.819778 "50M") ///
		ytitle(Effect of Winning Lottery Prize on Fertility) ///
		ylabel(-0.1(0.1)0.5, angle(0) format(%4.1f)) ///
		scheme(s1color) yline(0, lc(black))
graph export "$folder/figure/FigC2b.eps", as(eps) replace
graph export "$folder/figure/FigC2b.eps", as(eps) replace
